package com.tanhua.manage.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.manage.pojo.UserFreeze;
import com.tanhua.manage.service.UserManageService;
import com.tanhua.manage.vo.PageResult;
import com.tanhua.manage.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage/users")
@Slf4j
public class UserManageController {


    @Autowired
    private UserManageService userManageService;

    /**
     * 查询用户列表
     *
     * @param page     页码
     * @param pageSize 页大小
     * @param userId   用户id         模糊查询时候用
     * @param nickName 昵称           模糊查询时候用
     * @param city     城市           模糊查询时候用
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryUsers(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                                 @RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pageSize,
                                                 @RequestParam(value = "id", required = false) String userId,
                                                 @RequestParam(value = "nickname", required = false) String nickName,
                                                 @RequestParam(value = "city", required = false) String city) {


        try {
            PageResult pageResult = this.userManageService.queryUsers(page, pageSize, userId, nickName, city);

            if (ObjectUtil.isNotEmpty(pageResult)) {

                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询用户列表失败...", e);   //
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询用户基本信息
     * @param userId
     * @return
     */
    @GetMapping("{userID}")
    public ResponseEntity<UserInfoVo> queryUserInfo(@PathVariable("userID") String userId){

        try {
            UserInfoVo userInfoVo = this.userManageService.queryUserInfo(userId);

            if (ObjectUtil.isNotEmpty(userInfoVo)){
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("查询用户基本信息失败...id" + userId, e);  //
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 冻结
     * @param param
     * @return
     */
    @PostMapping("freeze")
    public ResponseEntity<String> freezeUser(@RequestBody Map<String, Object> param){


        try {

            UserFreeze userFreeze = JSONObject.parseObject(param.toString(),UserFreeze.class);

            //  UserFreeze userFreeze = JSONUtil.parseObj(param);

            if (ObjectUtil.isEmpty(userFreeze)){

                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }

           Boolean result = this.userManageService.freezeUser(userFreeze);



            if (result) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            log.error("操作失败", e);// e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");

    }


    /**
     * 解冻
     * @param param
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<String> unfreezeUser(@RequestBody Map<String, Object> param){


        String userId = (String) param.get("userId");
        String reasonsForThawing = (String) param.get("userId");

        try {

            Boolean result = this.userManageService.unfreezeUser(userId,reasonsForThawing);

            if (result) {
                return ResponseEntity.ok("操作成功");
            }

        } catch (Exception e) {
            log.error("操作失败", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");

    }

    }
